package com.zyy.service;

import com.zyy.handler.UserServiceInitializer;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import org.springframework.stereotype.Service;

/**
 * @author zyy
 * @date 2021/2/22 下午11:05
 * @description
 */
@Service
public class UserServiceImpl implements IUserService {

    //将来客户端要远程调用的方法
    @Override
    public String sayHello(String msg) {
        System.out.println("调用成功--参数" + msg);
        return "调用成功--参数" + msg;
    }

    //创建一个方法启动服务器
    public static void startServer(String ip, int port) throws InterruptedException {
        //1.创建两个线程组
        NioEventLoopGroup bosserGroup = new NioEventLoopGroup();
        NioEventLoopGroup workerGroup = new NioEventLoopGroup();

        //2.创建服务端的启动引导对象
        ServerBootstrap serverBootstrap = new ServerBootstrap();

        //3.配置启动引导对象
        serverBootstrap.group(bosserGroup,workerGroup)
                //设置通道为NIO
                .channel(NioServerSocketChannel.class)
                //创建监听channel
                .childHandler(new UserServiceInitializer());
        //4.绑定端口
        serverBootstrap.bind(ip,port).sync();

        System.out.println("服务启动...");
    }
}
